//
//  AddressVC.m
//  QoilPublic
//
//  Created by SunChao on 2018/1/27.
//  Copyright © 2018年 孙超. All rights reserved.
//

#import "AddressVC.h"
#import "AddressCell.h"
#import "ProtocolsCell.h"
@interface AddressVC ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic)  UITableView *tbView;
@property (strong, nonatomic) NSString *nameStr;
@property (strong, nonatomic) NSString *phoneStr;
@property (assign,nonatomic) BOOL isSelected;

@end

@implementation AddressVC
- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.backgroundColor = [UIColor groupTableViewBackgroundColor];
//        _tbView.scrollEnabled =NO; //设置tableview 不能滚动
//        _tbView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    }
    return _tbView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _isSelected = YES;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(buttonNotification) name:@"buttonNotification" object:nil];
    
     self.tbView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    self.tbView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tbView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [self.tbView addGestureRecognizer:tap];
    self.tbView.scrollEnabled = NO;
    // 定义一个button
    UIButton *footerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    // 为button设置frame
    footerButton.frame = CGRectMake(64, kScreenHeight - 120, kScreenWidth-128, 50);
    footerButton.layer.cornerRadius = 5;
    [footerButton setTitle:@"提交申请" forState:UIControlStateNormal];
    [footerButton setBackgroundColor:[UIColor colorWithRed:116.0/255.0 green:226.0/255.0 blue:44.0/255.0 alpha:1.0f]];
    [footerButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:footerButton];
    [self.tbView registerNib:[UINib nibWithNibName:@"AddressCell" bundle:nil] forCellReuseIdentifier:@"AddressCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"ProtocolsCell" bundle:nil] forCellReuseIdentifier:@"ProtocolsCell"];
}

- (void)buttonNotification{
    
    _isSelected = !_isSelected;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 50;
    }
    return 20;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *view = [[UIView alloc ]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(16, 0, kScreenWidth, 50)];
        
        label.text = @"个人信息";
        [view addSubview:label];
        
        return view;
        
    }else{
        
        return [[UIView alloc]init];
        
    }
  
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
   
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return 100;
    }
    return 50;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
     __weak AddressVC *wSelf = self;
    if (indexPath.section == 0) {
    
        AddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddressCell"];
        cell.nameBlock = ^(NSString *str) {
            __strong AddressVC *sSelf = wSelf;
            sSelf->_nameStr = str;
            CLog(@"姓名===%@",str);
        };
        cell.phoneBlock = ^(NSString *str){
            __strong AddressVC *sSelf = wSelf;
            sSelf->_phoneStr = str;
            CLog(@"电话===%@",str);
        };
        
    return cell;
        
    }else{
        
        ProtocolsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ProtocolsCell"];
        
        return cell;
        
    }
    
}
- (void )tap{
    
    [self.tbView endEditing:YES];
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [self.tbView endEditing:YES];
}
- (void)submitAction:(UIButton *)button{
    
    if (_isSelected == NO) {
        NSLog(@"请同意加油协议");
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:@"请同意加油协议" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        return;
    }
    if (_nameStr == nil||[_nameStr isEqualToString:@""]) {
        
        NSLog(@"请输入姓名");
         [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:@"请输入姓名" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        return;
    }
    if (_phoneStr == nil||[_phoneStr isEqualToString:@""]) {
        
        NSLog(@"请输入电话");
         [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:@"请输入电话" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
       
        return;
    }
    if (_phoneStr.length<11) {
        NSLog(@"请输入争取的手机号");
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:@"请输入正确电话" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
    }
    
    if (_isSelected) {
        if ([_nameStr length]>1&&[_phoneStr length]==11) {
            
            NSLog(@"提交成功");
            [self requestData];
            NSLog(@"%@,%@",_nameStr,_phoneStr);
            
        }
    }
}


- (void)requestData{
    
    NSString *url = [QParamService urlWithAction:@"userinfo/saveUir"];
    
    NSDictionary *param = [QParamService GainUserInfoWithName:_nameStr Phone:_phoneStr];
    
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        
        if(error){
            
            return ;
            
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            
             [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"提交成功" btnTitle:showAlertBtnTitle type:1 myBlock:^{
                 
                 [self.navigationController popViewControllerAnimated:YES];
                 
             }];
            
        }else{
            
             [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:@"提交失败" btnTitle:showAlertBtnTitle type:1 myBlock:^{
                 
                 [self.navigationController popViewControllerAnimated:YES];
                 
             }];
            
        }
    }];
    
}

@end

